
package com.study.services;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.study.model.mdao.DemantMapper;
import com.study.model.mdao.LogforcustDAO;
import com.study.model.mdao.OpportunityMapper;
import com.study.model.mdao.UserMDAO;
@Service
@Transactional(rollbackOn = Exception.class)
public class DemantService {
	@Autowired
	DemantMapper mapper;
	
	@Autowired
	LogforcustDAO logforcustDAO;
	
	@Autowired
	OpportunityMapper oppmapper;
	@Autowired
	UserMDAO udao;
	/**
	 * 带条件的分页查询
	 * @param pageNum
	 * @param pageSize
	 * @param name
	 * @param selects
	 * @param cusid 
	 * @return
	 */
	public List<JSONObject> selectBySel(int pageNum, int pageSize, String name, String selects,String useid, Integer cusid) {
		String mportance = null;
		PageHelper.startPage(pageNum, pageSize);
		if (selects.length() > 0 && selects.startsWith("321")) {
			mportance = selects;
		} 
		String names = "%"+name +"%";
		List<JSONObject> selectwhere = new ArrayList<JSONObject>();
		if ("6".equals(useid)) {
			return mapper.selectByWhere(mportance!=null?mportance.trim():mportance, names,cusid);
		}else {
			return mapper.selectByWhereOne(mportance!=null?mportance.trim():mportance, names,useid,cusid);

		}
		
	}
	/**
	 * 根据id查询需求
	 * @param id
	 * @return
	 */
	public JSONObject selectByid(String id) {
		return mapper.selectByid(Integer.parseInt(id));
	}
	/**
	 * 新增
	 * @param theme
	 * @param privoder
	 * @param custname
	 * @param opp
	 * @param degree
	 * @param content
	 * @param date
	 * @return
	 */
	public boolean insertdem(String theme, String privoder, String custid, String opp, String degree, String content,Date date,String useid) {
		int mportance = Integer.parseInt(degree);
		boolean flag = mapper.insertdem( theme,  privoder,  custid,  opp,  mportance,  content, date);
		 String name = udao.getfindUsreid(useid).getString("use_name");
	if (flag) {
		 String logMessage ="员工"+name+"在"+new Date()+"添加了一条客户需求信息";
		 int cusid = Integer.parseInt(custid);
			logforcustDAO.insert(cusid, new Date(), Integer.parseInt(useid), 2202, logMessage, "");
	}
		return flag;
	}
	/**
	 * 修改
	 * @param theme
	 * @param privoder
	 * @param custname
	 * @param opp
	 * @param degree
	 * @param content
	 * @param demid
	 * @return
	 */
	public boolean updatedemand(String theme, String privoder, String custname, String opp, String degree,
			String content,String demid,String useid) {
		boolean flag =mapper.updatedem(theme,privoder,custname,opp,Integer.parseInt(degree),content,Integer.parseInt(demid));
		 String name = udao.getfindUsreid(useid).getString("use_name");
		if (flag) {
			 String logMessage ="员工"+name+"在"+new Date()+"修改了  " +theme + "需求信息";
			 int cusid = Integer.parseInt(custname);
				logforcustDAO.insert(cusid, new Date(), Integer.parseInt(useid), 2202, logMessage, "");
		}
		return flag;
	}
	/**
	 * 根据销售机会查询对应的进度
	 * @param opp
	 * @return
	 */
	public List<JSONObject> selectopp(String opp) {
		 
		return mapper.selectopp(opp);
	}
	
	
	/**
	 * 根据销售机会查询对应的进度
	 * @param opp
	 * @return
	 */
	public List<JSONObject> selectopp1(String opp) {
		 
		return mapper.selectopp1(opp);
	}
	
	
	/**
	 * 根据销售机会查询对应的进度
	 * @param opp
	 * @return
	 */
	public List<JSONObject> selectopp2(String opp) {
		 
		return mapper.selectopp2(opp);
	}
	
	
	/**
	 * 根据销售机会查询对应的进度
	 * @param opp
	 * @return
	 */
	public List<JSONObject> selectopp3(String opp) {
		 
		return mapper.selectopp3(opp);
	}
	
	/**
	 * 将进度改为商务谈判
	 * @param opp
	 */
	public void updatedd(String opp) {
		
	 boolean flag=oppmapper.updatedd(opp);
	}
	
	/**
	 * 将进度改为投标竞争
	 * @param opp
	 */
	public void updatecom(String opp) {
		
		 boolean flag=oppmapper.updatecom(opp);
		}
	
	/**
	 * 将进度改为方案制定
	 * @param opp
	 */
	public void updatesolu(String opp) {
		
		 boolean flag=oppmapper.updatesolu(opp);
		}
	
	/**
	 * 将进度改为初期沟通
	 * @param opp
	 */
	public void updatedem(String opp) {
		
		 boolean flag=oppmapper.updatedem(opp);
		}
}

